/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.web.controller;

import java.io.File;
import java.io.Serializable;

import javax.annotation.PostConstruct;

import org.jefb.service.IFileSystemService;
import org.jefb.service.IUserSessionService;
import org.jefb.util.JefbUtils;
import org.jefb.web.model.DirectoryMananagerModel;
import org.jefb.web.model.MessagesModel;
import org.jefb.web.model.entity.Message;
import org.jefb.web.model.entity.MessageType;
import org.jefb.web.util.FileType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.zkoss.codemirror.Codemirror;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Borderlayout;
import org.zkoss.zul.South;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbar;
import org.zkoss.zul.Toolbarbutton;

@org.springframework.stereotype.Component
@Scope("prototype")
public class TextEditorController extends GenericForwardComposer implements
		Serializable {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private DirectoryMananagerModel directoryManagerModel;
	@Autowired
	private IUserSessionService userSessionService;
	@Autowired
	private MessagesModel messagesModel;
	@Autowired
	private IFileSystemService fileSystemService;

	private File textFile;

	// UI-components

	public TextEditorController() {

	}

	@PostConstruct
	public void init() {

	}

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		setTextFile(directoryManagerModel.getSelectedFile().getFile());
		createToolbar(comp);
	}

	private Textbox editorTextbox = new Textbox();
	private Toolbarbutton saveToolbarbutton = new Toolbarbutton();
	private Codemirror codemirror = new Codemirror();

	private void createToolbar(Component comp) {
		Toolbar editorToolbar = new Toolbar();
		editorToolbar.setParent(comp);

		saveToolbarbutton.setImage("../img/fs22/filesave.png");
		saveToolbarbutton.setParent(editorToolbar);
		// saveToolbarbutton.setDisabled(true);

		Borderlayout borderlayout = new Borderlayout();
		borderlayout.setParent(comp);
		South south = new South();
		south.setParent(borderlayout);
		south.setFlex(true);
		south.setSplittable(false);
		south.setSize("98%");

		editorTextbox.setMultiline(true);

		String extension = JefbUtils.extractExtension(textFile);
		FileType fileType = FileType.getByExtension(extension);
		if (FileType.getCodemirrorAllowedTextFiles().contains(fileType)) {
			codemirror.setParent(south);
			codemirror.setSyntax(fileType.getCodemirrorSyntax());
			codemirror.setVisible(true);
			// codemirror.addEventListener(Events.ON_CHANGING,
			// new TextChangedEventListener());
			editorTextbox.setVisible(false);
		} else {
			editorTextbox.setParent(south);
			editorTextbox.setVisible(true);
			// editorTextbox.addEventListener(Events.ON_CHANGING,
			// new TextChangedEventListener());
			codemirror.setVisible(false);

		}

		String fileContent = fileSystemService.getFileContent(textFile);
		editorTextbox.setValue(fileContent);
		codemirror.setValue(fileContent);

		// add event listener
		saveToolbarbutton.addEventListener(Events.ON_CLICK,
				new EventListener() {
					public void onEvent(Event event) throws Exception {
						if (codemirror.isVisible()) {
							fileSystemService.saveFileContent(textFile,
									codemirror.getValue());
						} else {
							fileSystemService.saveFileContent(textFile,
									editorTextbox.getValue());
						}
						messagesModel.addMessage(new Message("common.msg.data_saved",
								MessageType.SUCCESS));
					}
				});
	}

	public class TextChangedEventListener implements EventListener {
		public void onEvent(Event event) throws Exception {
			saveToolbarbutton.setDisabled(false);
		}
	}

	public void setDirectoryManagerModel(
			DirectoryMananagerModel directoryManagerModel) {
		this.directoryManagerModel = directoryManagerModel;
	}

	public DirectoryMananagerModel getDirectoryManagerModel() {
		return directoryManagerModel;
	}

	public void setUserSession(IUserSessionService userSessionService) {
		this.userSessionService = userSessionService;
	}

	public IUserSessionService getUserSession() {
		return userSessionService;
	}

	public void setTextFile(File textFile) {
		this.textFile = textFile;
	}

	public File getTextFile() {
		return textFile;
	}

	public void setFileSystemService(IFileSystemService fileSystemService) {
		this.fileSystemService = fileSystemService;
	}

	public IFileSystemService getFileSystemService() {
		return fileSystemService;
	}

	public void setMessagesModel(MessagesModel messagesModel) {
		this.messagesModel = messagesModel;
	}

	public MessagesModel getMessagesModel() {
		return messagesModel;
	}

}
